package com.hotel.front;

import java.io.IOException;
import java.text.ParseException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.Util;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.labs.repackaged.org.json.JSONException;
import com.google.gson.Gson;
import com.hotel.BL.AlphabetCodingLogic;
import com.hotel.BL.GenericLogic;

import data.passingData.DisplayColorData;
import data.passingData.ResultData;

@SuppressWarnings("serial")
public class DisplayColorService extends HttpServlet {
	// http://localhost:8080/CreditCardService?func=addCreditCard&CVC=123&cardType=Credit
	// Card&NameOnCard=test&cardNumber=123456&ExpiryDate=07/56
	// http://localhost:8080/CreditCardService?func=editCreditCard&creditCardId=865&CVC=123&cardType=Credit
	// Card&NameOnCard=test&cardNumber=123456&ExpiryDate=07/56
	// http://localhost:8080/CreditCardService?func=delCreditCard&creditCardId=865
	// http://localhost:8080/CreditCardService?func=changeCreditCard&creditCardId=865
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		try {
			String doClass = req.getParameter("func");
			if (doClass.equals("loadDisplayColor")) {
				LoadDisplayColor(req, resp);
			} else if (doClass.equals("editDisplayColor")) {
				EditDisplayColor(req, resp);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void LoadDisplayColor(HttpServletRequest req, HttpServletResponse resp)
			throws IOException, JSONException, ServletException,
			EntityNotFoundException, ParseException {
		HttpSession session=req.getSession(true);
		Gson gson = new Gson();
		String sessionCheck = Util.checkSessionEntity(req,resp);
		if(sessionCheck==null||sessionCheck.equals("")){
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(new ResultData(false)));
			return;
		}
		long hotelId = (Long) session.getAttribute("hotelId");
		DisplayColorData dcd = new DisplayColorData();
		GenericLogic gLogic = new GenericLogic(req,resp);
		AlphabetCodingLogic alphabetCodingLogic = new AlphabetCodingLogic();
		
		dcd.setCheckin(gLogic.GetReservationStatus(hotelId, "Checked-in Rooms").getColor());
		dcd.setCheckout(gLogic.GetReservationStatus(hotelId, "Checked-out Rooms").getColor());
		dcd.setReserved(gLogic.GetReservationStatus(hotelId, "Reserved Rooms").getColor());
		dcd.setTemp(gLogic.GetReservationStatus(hotelId, "Temp Rooms").getColor());
		
		dcd.setClean(gLogic.GetCleaningStatus(hotelId, "Clean Rooms").getColor());
		dcd.setDirty(gLogic.GetCleaningStatus(hotelId, "Dirty Rooms").getColor());
		dcd.setInspect(gLogic.GetCleaningStatus(hotelId, "Inspect Rooms").getColor());
		dcd.setDnr(gLogic.GetCleaningStatus(hotelId, "DNR").getColor());
		dcd.setRepair(gLogic.GetCleaningStatus(hotelId, "Repair Rooms").getColor());
		dcd.setTouchup(gLogic.GetCleaningStatus(hotelId, "Touch-Up Rooms").getColor());
		
		dcd.setGroupbooking(alphabetCodingLogic.GetAlphabet(hotelId, "Group Booking").getAlphabet());
		dcd.setOnlinebooking(alphabetCodingLogic.GetAlphabet(hotelId, "Online/Web Booking").getAlphabet());
		dcd.setOtherbooking(alphabetCodingLogic.GetAlphabet(hotelId, "Other/GDS Booking").getAlphabet());
		dcd.setTrvelagentbooking(alphabetCodingLogic.GetAlphabet(hotelId, "Travel Agent Booking").getAlphabet());
		dcd.setCoperatebooking(alphabetCodingLogic.GetAlphabet(hotelId, "Corporate Booking").getAlphabet());

		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(dcd));
	}
	
	private void EditDisplayColor(HttpServletRequest req, HttpServletResponse resp)
			throws IOException, JSONException, ServletException,
			EntityNotFoundException, ParseException {
		HttpSession session=req.getSession(true);
		Gson gson = new Gson();
		String sessionCheck = Util.checkSessionEntity(req,resp);
		if(sessionCheck==null||sessionCheck.equals("")){
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(new ResultData(false)));
			return;
		}
		long hotelId = (Long) session.getAttribute("hotelId");
		
		//room status
		String checkin = req.getParameter("checkin");
		String checkout = req.getParameter("checkout");
		String reserved = req.getParameter("reserved");
		String temp = req.getParameter("temp");

		GenericLogic gLogic = new GenericLogic(req,resp);
		AlphabetCodingLogic alphabetCodingLogic = new AlphabetCodingLogic();
		long idcheckin = gLogic.GetReservationStatus(hotelId, "Checked-in Rooms").getId();
		long idcheckOut = gLogic.GetReservationStatus(hotelId, "Checked-out Rooms").getId();
		long idreserved = gLogic.GetReservationStatus(hotelId, "Reserved Rooms").getId();
		long idtemp = gLogic.GetReservationStatus(hotelId, "Temp Rooms").getId();
		
		alphabetCodingLogic.EditReservationStatusColor(idcheckin, checkin);
		alphabetCodingLogic.EditReservationStatusColor(idcheckOut, checkout);
		alphabetCodingLogic.EditReservationStatusColor(idreserved, reserved);
		alphabetCodingLogic.EditReservationStatusColor(idtemp, temp);
		//cleaning status
		String clean = req.getParameter("clean");
		String dirty = req.getParameter("dirty");
		String inspect = req.getParameter("inspect");
		String dnr = req.getParameter("dnr");
		String repair = req.getParameter("repair");
		String touchup = req.getParameter("touchup");

		long idclean = gLogic.GetCleaningStatus(hotelId, "Clean Rooms").getId();
		long iddirty = gLogic.GetCleaningStatus(hotelId, "Dirty Rooms").getId();
		long idinspect = gLogic.GetCleaningStatus(hotelId, "Inspect Rooms").getId();
		long iddnr = gLogic.GetCleaningStatus(hotelId, "DNR").getId();
		long idrepair = gLogic.GetCleaningStatus(hotelId, "Repair Rooms").getId();
		long idtouchup = gLogic.GetCleaningStatus(hotelId, "Touch-Up Rooms").getId();
		
		long idcleaningdnr = gLogic.GetReservationStatus(hotelId, "DNR").getId();
		gLogic.EditReservationStatusColor(idcleaningdnr, dnr);
		
		alphabetCodingLogic.EditCleaningStatusColor(idclean, clean);
		alphabetCodingLogic.EditCleaningStatusColor(iddirty, dirty);
		alphabetCodingLogic.EditCleaningStatusColor(idinspect, inspect);
		alphabetCodingLogic.EditCleaningStatusColor(iddnr, dnr);
		alphabetCodingLogic.EditCleaningStatusColor(idrepair, repair);
		alphabetCodingLogic.EditCleaningStatusColor(idtouchup, touchup);
		
		//alphabet
		String group = req.getParameter("group");
		String online = req.getParameter("online");
		String other = req.getParameter("other");
		String travel = req.getParameter("travel");
		String coperate = req.getParameter("coperate");
		
		long idgroup = alphabetCodingLogic.GetAlphabet(hotelId, "Group Booking").getId();
		long idonline = alphabetCodingLogic.GetAlphabet(hotelId, "Online/Web Booking").getId();
		long idother = alphabetCodingLogic.GetAlphabet(hotelId, "Other/GDS Booking").getId();
		long idtravel = alphabetCodingLogic.GetAlphabet(hotelId, "Travel Agent Booking").getId();
		long idcoperate = alphabetCodingLogic.GetAlphabet(hotelId, "Corporate Booking").getId();
		
		alphabetCodingLogic.EditAlphabetCoding(idgroup, group);
		alphabetCodingLogic.EditAlphabetCoding(idonline, online);
		alphabetCodingLogic.EditAlphabetCoding(idother, other);
		alphabetCodingLogic.EditAlphabetCoding(idtravel, travel);
		alphabetCodingLogic.EditAlphabetCoding(idcoperate, coperate);
		
		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(true));
	}
}